package cn.doitedu.producer.high.datasource;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.util.Iterator;

public class RandomDataSource implements Iterable<String> {
    String limit;
    public RandomDataSource( String limit){
        this.limit = limit;
    }


    @Override
    public Iterator<String> iterator() {
        return new Itr(limit);
    }


    private class Itr implements Iterator<String> {

        private int limit;
        private int count;
        private String template = "{\"user_id\":%s,\"event_id\":\"%s\",\"properties\":{\"f1\":%.2f,\"f2\":%.2f,\"f3\":%.2f}}";

        public Itr(String limit) {
            this.limit = Integer.parseInt(limit);
        }

        @Override
        public boolean hasNext() {
            return count < limit;
        }

        @Override
        public String next() {


            String userId = RandomStringUtils.randomNumeric(3);
            String eventId = RandomStringUtils.randomAlphabetic(4).toUpperCase();

            double f1 = RandomUtils.nextDouble(10,100);
            double f2 = RandomUtils.nextDouble(10,100);
            double f3 = RandomUtils.nextDouble(10,100);

            String res = String.format(template, userId, eventId, f1, f2, f3);


            count++;

            return res;
        }
    }



}
